package net.jcip.examples.ReentrantLock;

import net.jcip.annotations.ThreadSafe;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLockPseudoRandom
 * <p/>
 * Random number generator using ReentrantLock
 *
 * @author Brian Goetz and Tim Peierls
 */
@ThreadSafe
public class ReentrantLockAdd extends PseudoRandom {
    private final Lock lock = new ReentrantLock(false);
    private int init;

    ReentrantLockAdd(int seed) {
        this.init = seed;
    }

    public int nextInt() {
        lock.lock();
        try {
            return ++init;
        } finally {
            lock.unlock();
        }
    }
}
